package cc.orange.mainView;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import cc.orange.adapter.AppraiseAdapter;
import cc.orange.base.BaseFragment;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.PersonMsgEntity;
import cc.orange.http.ApiService;

import cc.orange.databinding.FragmentAppraiseBinding;

import cc.orange.R;

import cc.orange.utils.ZXToastUtil;

public class AppraiseFragment extends BaseFragment {

    private AppraiseAdapter adapter;
    private List<PersonMsgEntity.Data.PingJia>list = new ArrayList<>();
    private FragmentAppraiseBinding binding;
    private PersonMsgEntity baseEntity ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_appraise,container,false);
        initViews();
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getData(PersonMsgEntity baseEntity){
        if(null != baseEntity.getData() && baseEntity.getData().getPingJia().size() > 0){
            this.baseEntity = baseEntity;
            list.clear();
            list.addAll(baseEntity.getData().getPingJia());
            adapter.notifyDataSetChanged();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews(){

//        for (int i = 0; i < 10; i++) {
//            list.add(new HightEntity(0));
//        }
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        binding.msgaRecycler.setLayoutManager(layoutManager);
        adapter = new AppraiseAdapter(list);
        binding.msgaRecycler.setAdapter(adapter);

        adapter.setItemClickListener(new AppraiseAdapter.ItemClickListener() {
            @Override
            public void itemClickListenr(int position) {
                //TODO 标签评价接口  和评价状态判断
                evaluate(""+list.get(position).getId(),""+baseEntity.getData().getId(),
                        list.get(position).getStatus() == 0 ? 1 : 0,position);
            }
        });

//        //下拉刷新
//        binding.smartRL.setRefreshHeader(new ClassicsHeader(getActivity()));
//        binding.smartRL.setRefreshFooter(new ClassicsFooter(getActivity()));
//        binding.smartRL.setEnableLoadMore(true);
//        binding.smartRL.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                binding.smartRL.finishRefresh(3);
//            }
//        });
//        binding.smartRL.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                binding.smartRL.finishLoadMore(3);
//            }
//        });
    }

    private void evaluate(String dId,String uId,int flag,int position){//flag为点赞标识 1不点赞 0点赞
        RetrofitFactory.create(ApiService.class)
                .evaluate(getTokens(),dId,uId,""+flag)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.get(position).setStatus(flag);
                            adapter.notifyDataSetChanged();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


    @Override
    public void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}